import UIKit
import SwiftUI

class KeyboardViewController: UIInputViewController {
    
    // 在 viewDidLoad 方法中，调整键盘高度
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 创建 SwiftUI 视图
        let keyboardView = KeyboardView(proxy: textDocumentProxy)
        let hostingController = UIHostingController(rootView: keyboardView)
        
        // 添加为子视图控制器
        addChild(hostingController)
        view.addSubview(hostingController.view)
        hostingController.didMove(toParent: self)
        
        // 设置约束
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            hostingController.view.leftAnchor.constraint(equalTo: view.leftAnchor),
            hostingController.view.rightAnchor.constraint(equalTo: view.rightAnchor),
            hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        // 设置键盘高度 - 增加高度以适应新的布局
        view.heightAnchor.constraint(equalToConstant: 350).isActive = true
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.view.backgroundColor = .systemBackground
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // 确保视图在显示后可见
        view.backgroundColor = .systemBackground
    }
    
    override func textWillChange(_ textInput: UITextInput?) {
        // 文本即将改变时的处理
    }
    
    override func textDidChange(_ textInput: UITextInput?) {
        // 文本已经改变时的处理
    }
}
